package com.neuedu.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.neuedu.entity.Bed;
import com.neuedu.entity.Beddetails;
import com.neuedu.entity.Customer;
import com.neuedu.entity.Room;
import com.neuedu.mapper.BeddetailsMapper;
import com.neuedu.params.BedDetaisParams;
import com.neuedu.service.BedService;
import com.neuedu.service.BeddetailsService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.neuedu.service.CustomerService;
import com.neuedu.service.RoomService;
import jakarta.annotation.Resource;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.transaction.TransactionStatus;
import org.springframework.transaction.support.DefaultTransactionDefinition;

import java.time.LocalDate;
import java.util.List;

/**
 * <p>
 * 床位信息管理表 服务实现类
 * </p>
 *
 * @author wangyu
 * @since 2025-10-16
 */
@Service
public class BeddetailsServiceImpl extends ServiceImpl<BeddetailsMapper, Beddetails> implements BeddetailsService {
    @Resource
    BedService bedService;
    @Resource
    CustomerService customerService;
    @Resource
    RoomService roomService;
    @Resource
    PlatformTransactionManager transactionManager;
    @Override
    public IPage<Beddetails> list(Integer pageNo, Integer pageSize, String name, LocalDate startDate, LocalDate endDate, Integer isDeleted) {
        BedDetaisParams params = new BedDetaisParams();
        params.setIsDeleted(isDeleted);
        if (StringUtils.isNoneBlank(name)) {
            params.setName(name);
        }
        if (null != startDate) {
            params.setStartDate(startDate);
        }
        if (null != endDate) {
            params.setEndDate(endDate);
        }
        System.out.println(params);
        return this.baseMapper.getList(new Page(pageNo, pageSize), params.getName(), params.getStartDate(), params.getEndDate(), params.getIsDeleted());
    }

    @Override
    public Beddetails getById(Integer id) {
        return this.baseMapper.getById(id);
    }

    @Override
    public boolean update(Beddetails beddetails, Integer roomNo, Integer newbedId) {
        // 通过newBedId 查询单条信息
        Bed bed = bedService.getById(newbedId);
        // 通过roomNo 获取 单条信息
        Room room = roomService.getById(roomNo);
        // 在beddetails表中更新bedId和bed_Details
        Beddetails beddetailsEntry = new Beddetails();
        beddetailsEntry.setId(beddetails.getId());
        beddetailsEntry.setBedId(newbedId);
        beddetailsEntry.setBedDetails(String.format("%d#%s", room.getRoomNo(), bed.getBedNo()));
        // 拿着customerId 到customer表中 更新 roomNo和bedId
        Customer customer = new Customer();
        customer.setId(beddetails.getCustomerId());
        customer.setRoomNo(String.valueOf(roomNo));
        customer.setBedId(newbedId);
        // 到bed中 将 原先的bedId 更新为空闲状态  newBedId 更新为入住状态
        Bed oldBed = new Bed();
        oldBed.setId(beddetails.getBedId());
        oldBed.setBedStatus(1);
        Bed newBed = new Bed();
        newBed.setId(newbedId);
        newBed.setBedStatus(2);
        // 定义事务
        DefaultTransactionDefinition definition = new DefaultTransactionDefinition();
        // 开启事务
        TransactionStatus status = transactionManager.getTransaction(definition);
        try {
            this.updateById(beddetailsEntry);
            customerService.updateById(customer);
            bedService.updateById(oldBed);
            bedService.updateById(newBed);
            transactionManager.commit(status);
            return true;
        } catch (Exception e) {
            transactionManager.rollback(status);
            throw e;
        }
    }
}
